package lib

import (
	"fmt"
	"io/ioutil"
	"regexp"
	"strings"
	"time"
)

func SearchStock(msg string) (str string) {
	resp, err := HttpGet(fmt.Sprintf("http://suggest3.sinajs.cn/suggest/type=&key=%s&name=suggestdata_%d", msg, time.Now().UnixNano()))
	if err != nil {
		return
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	str = string(body)

	reg := regexp.MustCompile("\"([^\"]*)\"")
	s := reg.FindStringSubmatch(str)
	if len(s) >= 2 {
		var tmp []string
		name := strings.Split(s[1], ";")
		for k, v := range name {
			if k == 0 {
				tmp = strings.Split(v, ",")
				if len(tmp) >= 4 {
					str = tmp[3]
					return
				}
			}
		}
	}
	return
}

func CheckImageUrl(imgUrl string) bool {
	resp, err := HttpGet(imgUrl)
	if err != nil {
		return false
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return false
	}
	return string(body) != ""
}
